package com.xiumiing.uilibrary.base;

import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.xiumiing.uilibrary.R;
import com.xiumiing.uilibrary.rx.RxManager;

import static com.xiumiing.uilibrary.Constans.EXIT;


public class BaseActivity extends AppCompatActivity {

    private Toolbar mToolbar;//顶部栏目
    private AppBarLayout mAppBar;
    private TextView mTvRight;
    private TextView mTvTitle;
    public ImmersionBar mImmersionBar;
    public RxManager mRxManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        doBeforeSetcontentView();
        mRxManager = new RxManager();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        initBar();
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initBar();
    }

    /**
     * 右边导航 按钮
     */
    protected void setToolbarRightBackgroundResource(@DrawableRes int resid, int wDp, int hDp) {
        if (mTvRight == null) return;
        mTvRight.setVisibility(View.VISIBLE);
        ViewGroup.LayoutParams params = mTvRight.getLayoutParams();
        params.height = SizeUtils.dp2px(hDp);
        params.width = SizeUtils.dp2px(wDp);
        mTvRight.setLayoutParams(params);
        mTvRight.setBackgroundResource(resid);
    }

    /**
     * 右边导航 按钮
     */
    protected void setToolbarRightBackgroundResource(@DrawableRes int resid) {
        setToolbarRightBackgroundResource(resid, 29, 29);
    }

    /**
     * 右边导航 按钮
     */
    protected void setToolbarRightText(CharSequence text, @ColorInt int color, float size) {
        if (mTvRight == null) return;
        mTvRight.setVisibility(View.VISIBLE);
        ViewGroup.LayoutParams params = mTvRight.getLayoutParams();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        mTvRight.setLayoutParams(params);
        mTvRight.setText(text);
        mTvRight.setTextColor(color);
        mTvRight.setTextSize(size);
    }

    /**
     * 右边导航 按钮
     */
    protected void setToolbarRightText(CharSequence text) {
        setToolbarRightText(text, getResources().getColor(R.color.white), 14);
    }

    /**
     * 初始化顶部栏
     */
    public void initBar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mAppBar = (AppBarLayout) findViewById(R.id.app_bar_layout);
        mTvRight = (TextView) findViewById(R.id.tv_right);

        if (mToolbar == null || mAppBar == null) {
            LogUtils.w("没有发现toolbar，请导入toolbar");
            return;
        }
        //设置返回按钮
//        mToolbar.setNavigationIcon();
        setSupportActionBar(mToolbar);
        //将toolbar文字移动到中间
        mTvTitle = (TextView) mToolbar.findViewById(R.id.title);
        mTvTitle.setText(getTitle());
        getSupportActionBar().setTitle(null);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    finishAfterTransition();
                } else {
                    finish();
                }
            }
        });
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        if (Build.VERSION.SDK_INT >= 21) {
            mAppBar.setElevation(10.6f);
        }

    }


    /**
     * 设置layout前配置
     */
    private void doBeforeSetcontentView() {
        // 把actvity放到application栈中管理
        AppManager.getAppManager().addActivity(this);
        // 无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 默认着色状态栏
        SetStatusBarColor();
    }

    /**
     * 沉浸式状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.colorPrimary, 0.0f);
        mImmersionBar.init();   //所有子类都将继承这些相同的属性
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //必须调用该方法，防止内存泄漏，不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
        if (mImmersionBar != null)
            mImmersionBar.destroy();
        mRxManager.clear();
        AppManager.getAppManager().finishActivity(this);
    }

    /**
     * 记录退出时间差
     */
    private long mPressedTime = 0;

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public void onExit() {
        //获取第一次按键时间
        long mNowTime = System.currentTimeMillis();
        //比较两次按键时间差
        if ((mNowTime - mPressedTime) > EXIT) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mPressedTime = mNowTime;
        } else {//退出程序
            AppManager.getAppManager().finishAllActivity();
            System.exit(0);
        }
    }

}
